package com.ioidea.holland.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.BarUtils;
import com.ioidea.base.util.DeviceUtil;
import com.ioidea.holland.R;


/**
 * @author tjh
 */
public class CommonTitleBar extends LinearLayout {

    private Context mContext;
    private View statusBarView;
    private String titleText;
    private String rightText;
    private int titleColor;
    private int rightTextColor;
    private int baColor;
    private int backDrawable;
    private boolean isRightTvVisible;
    private int rightIvResId;
    private boolean isRightIvVisible;

    private ImageView imgBack;
    public TextView tvTitle;
    private TextView tvRight;
    private ImageView ivRight;
    private OnRightTvClickListener onRightTvClickListener;
    private OnRightIvClickListener onRightIvClickListener;
    private boolean isLeftIvVisible;
    private TextView tvLeft;
    private boolean isLeftTvVisible;
    private String leftText;
    private ImageView ivRightTwo;
    private boolean isRightIv2Visible;
    private int rightIv2ResId;

    public CommonTitleBar(Context context) {
        this(context, null);
    }

    public CommonTitleBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonTitleBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTypedArray(context, attrs);
        initView();
    }

    private void initTypedArray(Context context, AttributeSet attrs) {
        mContext = context;
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
        titleText = mTypedArray.getString(R.styleable.TitleBar_title_text);
        rightText = mTypedArray.getString(R.styleable.TitleBar_right_tv_text);
        leftText = mTypedArray.getString(R.styleable.TitleBar_left_tv_text);
        titleColor = mTypedArray.getColor(R.styleable.TitleBar_title_text_color, Color.parseColor("#333333"));
        rightTextColor = mTypedArray.getColor(R.styleable.TitleBar_right_text_color, Color.parseColor("#666666"));
        isRightTvVisible = mTypedArray.getBoolean(R.styleable.TitleBar_right_tv_visible, false);
        isLeftTvVisible = mTypedArray.getBoolean(R.styleable.TitleBar_left_tv_visible, false);
        isLeftIvVisible = mTypedArray.getBoolean(R.styleable.TitleBar_left_iv_visible, true);
        isRightIvVisible = mTypedArray.getBoolean(R.styleable.TitleBar_right_iv_visible, false);
        isRightIv2Visible = mTypedArray.getBoolean(R.styleable.TitleBar_right_iv2_visible, false);
        backDrawable = mTypedArray.getResourceId(R.styleable.TitleBar_back_src, R.drawable.ic_white_back );
        rightIvResId = mTypedArray.getResourceId(R.styleable.TitleBar_right_iv_src, R.drawable.screen_list_icon);
        rightIv2ResId = mTypedArray.getResourceId(R.styleable.TitleBar_right_iv2_src, R.drawable.screen_list_icon);
        baColor = mTypedArray.getColor(R.styleable.TitleBar_background_color, Color.parseColor("#ffffff"));
        mTypedArray.recycle();
    }

    private void initView() {
        inflate(getContext(), R.layout.common_title_bar, this);
        setBackgroundColor(baColor);
        statusBarView = findViewById(R.id.status_bar_view);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) statusBarView.getLayoutParams();
        layoutParams.height = BarUtils.getStatusBarHeight();
        imgBack = findViewById(R.id.back_iv);
        tvLeft = findViewById(R.id.left_tv);

        tvTitle = findViewById(R.id.title_tv);
        tvTitle.getPaint().setFakeBoldText(true);
        tvRight = findViewById(R.id.right_tv);
        ivRight = findViewById(R.id.right_iv);
        ivRightTwo = findViewById(R.id.right_iv_two);
        imgBack.setImageResource(backDrawable);
        imgBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLeftIvClickListener != null) {
                    onLeftIvClickListener.onClick();
                } else {
                    ((Activity) getContext()).finish();
                }

            }
        });
        tvTitle.setTextColor(titleColor);
        tvTitle.setText(titleText);
        tvRight.setTextColor(rightTextColor);
        tvRight.setText(rightText);
        tvRight.setVisibility(isRightTvVisible ? View.VISIBLE : View.GONE);
        tvRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRightTvClickListener != null) {
                    onRightTvClickListener.onClick();
                }
            }
        });
        ivRight.setImageResource(rightIvResId);
        ivRight.setVisibility(isRightIvVisible ? View.VISIBLE : View.GONE);
        ivRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRightIvClickListener != null) {
                    onRightIvClickListener.onClick();
                }
            }
        });

        ivRightTwo.setImageResource(rightIv2ResId);
        ivRightTwo.setVisibility(isRightIv2Visible ? View.VISIBLE : View.GONE);
        ivRightTwo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRightIv2ClickListener != null) {
                    onRightIv2ClickListener.onClick();
                }
            }
        });


        tvLeft.setTextColor(titleColor);
        tvLeft.setText(leftText);
        tvLeft.setVisibility(isLeftTvVisible ? View.VISIBLE : View.GONE);
        imgBack.setVisibility(isLeftIvVisible ? View.VISIBLE : View.GONE);
    }


    public interface OnLeftIvClickListener {
        void onClick();
    }


    public interface OnRightIvClickListener {
        void onClick();
    }

    public interface OnRightIv2ClickListener {
        void onClick();
    }

    public interface OnRightTvClickListener {
        void onClick();
    }

    OnRightIv2ClickListener onRightIv2ClickListener;
    OnLeftIvClickListener onLeftIvClickListener;

    public void setOnLeftIvClickListener(OnLeftIvClickListener onLeftIvClickListener) {
        this.onLeftIvClickListener = onLeftIvClickListener;
    }


    public void setOnRightIvClickListener(OnRightIvClickListener onRightIvClickListener) {
        this.onRightIvClickListener = onRightIvClickListener;
    }

    public void setOnRightIv2ClickListener(OnRightIv2ClickListener onRightIv2ClickListener) {
        this.onRightIv2ClickListener = onRightIv2ClickListener;
    }

    public void setOnRightTvClickListener(OnRightTvClickListener onRightTvClickListener) {
        this.onRightTvClickListener = onRightTvClickListener;
    }


    public String getRightText() {
        return rightText;
    }

    public void setRightText(String rightText) {
        this.rightText = rightText;
        tvRight.setText(rightText);
    }

//    public void setRightText(String rightText) {
//        this.rightText = rightText;
//        tvRight.setText(rightText);
//    }

    public void setRightIv(int rightIvResId) {
        this.rightIvResId = rightIvResId;
        ivRight.setImageResource(rightIvResId);
    }

    public void setLeftIv(int leftIvResId) {
        imgBack.setImageResource(leftIvResId);
    }


    public void setRightText(String rightText, String tvColor) {
        this.rightText = rightText;
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText(rightText);
        tvRight.setTextColor(Color.parseColor(tvColor));
    }


    public void setTitleText(String titleText) {
        this.titleText = titleText;
        tvTitle.setText(titleText);
    }

    public void setLeftText(String leftText) {
        this.leftText = leftText;
        tvLeft.setText(leftText);
    }

    public void setTitleText(String titleText, Drawable drawable) {
        this.titleText = titleText;
        tvTitle.setText(titleText);
        tvTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, drawable, null);
        tvTitle.setCompoundDrawablePadding(DeviceUtil.dip2px(getContext(), 3));
    }

    public void setRightIvVisible(boolean isVisible) {
        ivRight.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    public void setRightIv2Visible(boolean isVisible) {
        ivRightTwo.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    public void setRightTvVisible(boolean isVisible) {
        tvRight.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    //getResources().getDrawable(R.drawable.ico_delete)
    public void setLeftTvDrawable(String txt, Drawable drawable) {
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null);
        tvRight.setCompoundDrawablePadding(DeviceUtil.dip2px(getContext(), 3));
        tvRight.setText(txt);
    }

    public TextView getTitleTextView() {
        return tvTitle;
    }

    public ImageView getImgBack() {
        return imgBack;
    }
}
